﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySpace.Data;
using DataBaseTable;

public partial class Manage_GuestBook_List : System.Web.UI.Page
{
    protected static int nowpages;
    GuestBookSetting gbs = new GuestBookSetting();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            nowpages = 1;
            this.bdguestbook();
        }
    }
    protected void bdguestbook()
    {
        DBHandler dbh = GetHandler.Init();
        dbh.CommandText = "select * from hwguestbook order by IsTop desc,ID desc";
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = dbh.ExecuteDataTable().DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = gbs.PageSize;
        toone.Enabled = true;
        toup.Enabled = true;
        tonext.Enabled = true;
        tolast.Enabled = true;
        if (nowpages == 1)
        {
            toone.Enabled = false;
            toup.Enabled = false;
        }
        if (nowpages == ps.PageCount)
        {
            tonext.Enabled = false;
            tolast.Enabled = false;
        }
        if (!IsPostBack)
        {
            for (int i = 1; i <= ps.PageCount; i++)
            {
                jumppage.Items.Add(i.ToString());
            }
        }
        curpage.Text = nowpages.ToString();
        sumpage.Text = ps.PageCount.ToString();
        tpagesizes.Text = ps.PageSize.ToString();
        pagesum.Text = ps.DataSourceCount.ToString();
        ps.CurrentPageIndex = nowpages - 1;//获取当前页码
        guest_list_rp.DataSource = ps;
        guest_list_rp.DataBind();

    }
    protected void toone_Click(object sender, EventArgs e)
    {
        nowpages = 1;
        this.bdguestbook();
    }
    protected void toup_Click(object sender, EventArgs e)
    {
        --nowpages;
        this.bdguestbook();
    }
    protected void tonext_Click(object sender, EventArgs e)
    {
        ++nowpages;
        this.bdguestbook();
    }
    protected void jumppage_SelectedIndexChanged(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(((DropDownList)sender).SelectedValue);
        this.bdguestbook();
    }
    protected void tolast_Click(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(sumpage.Text);
        this.bdguestbook();
    }
    protected void guest_list_rp_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "reply")
        {
            HiddenField id_hf = (HiddenField)e.Item.FindControl("id_hf");
            TextBox Content_tb = (TextBox)e.Item.FindControl("Content_tb");
            TextBox Reply_tb = (TextBox)e.Item.FindControl("Reply_tb");
            CheckBox ToTop_cb = (CheckBox)e.Item.FindControl("ToTop_cb");
            GuestBookTable gbt = new GuestBookTable(Convert.ToInt32(id_hf.Value));
            gbt.Content = Content_tb.Text;
            gbt.Reply = Reply_tb.Text;
            if (ToTop_cb.Checked)
            {
                gbt.IsTop = 1;
            }
            else
            {
                gbt.IsTop = 0;
            }
            try
            {
                gbt.Update();
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('更新留言成功');</script>");
            }
            catch
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('更新留言失败');</script>");
            }
        }
        if (e.CommandName == "del")
        {
            HiddenField id_hf = (HiddenField)e.Item.FindControl("id_hf");
            GuestBookTable gbt = new GuestBookTable(Convert.ToInt32(id_hf.Value));
            try
            {
                gbt.Delete();
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('删除留言成功');</script>");
                this.bdguestbook();
            }
            catch
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('删除留言失败');</script>");
            }
        }
    }
}